package com.witmed.home.view


import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.witmed.home.R
import com.witmed.home.databinding.ActivityDetailBinding
import com.witmed.home.view.intent.VideoIntent
import com.witmed.home.view.viewmodel.VideoState
import com.witmed.home.view.viewmodel.VideoViewModel
import com.witmed.lib_common.ActivityPath
import com.witmed.lib_common.room.VideoHistory
import com.witmed.lib_http.entity.VideoListItem
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date


@Route(path=ActivityPath.PAGE_DETAIL)
class DetailActivity : AppCompatActivity() {
    @Autowired
    lateinit var video:VideoListItem
    lateinit var db:ActivityDetailBinding
    lateinit var vp:StandardGSYVideoPlayer
    lateinit var vm:VideoViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db=ActivityDetailBinding.inflate(layoutInflater)
        setContentView(db.root)
        vm=ViewModelProvider(this).get(VideoViewModel::class.java)

        vp=db.videoPlayer

        ARouter.getInstance().inject(this)

        //隐藏标题
        vp.titleTextView.visibility=View.GONE
        vp.backButton.visibility=View.GONE

        //设置路径，是否缓存，标题
        vp.setUp(video.videopath,true,video.title)
        //播放
        vp.startPlayLogic()

        lifecycleScope.launch {
            val calendar=Calendar.getInstance()
            val year = calendar.get(Calendar.YEAR)
            val month=calendar.get(Calendar.MONTH)+1
            val day=calendar.get(Calendar.DAY_OF_MONTH)
            val hour=calendar.get(Calendar.HOUR_OF_DAY)
            val minute=calendar.get(Calendar.MINUTE)
            val second=calendar.get(Calendar.SECOND)
            val videoHistory = VideoHistory(
                video.id, video.title, video.videopath,video.videomainimag,
                "$year-$month-$day $hour:$minute:$second"
            )
            vm.videoChannel.send(VideoIntent.insetVideo(videoHistory,this@DetailActivity))
        }

    }

    override fun onPause() {
        super.onPause()
        //暂停
        vp.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        //继续
        vp.onVideoResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        //重置所有视频资源
        GSYVideoManager.releaseAllVideos()

    }
}